package cn.sdormitory.canteen.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.sdormitory.canteen.dao.FoodGoodsCheckDao;
import cn.sdormitory.canteen.dao.FoodOrderDao;
import cn.sdormitory.canteen.dao.FoodOrderGoodsDao;
import cn.sdormitory.canteen.service.FoodOrderService;
import cn.sdormitory.canteen.vo.*;
import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.utils.SysUserUtils;
import cn.sdormitory.sys.dao.SysNoticeDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

@Service("foodOrderService")
public class FoodOrderServiceImpl extends ServiceImpl<FoodOrderDao, FoodOrderVO> implements FoodOrderService {

    @Autowired
    private FoodOrderDao foodOrderDao;
    @Autowired
    private FoodOrderGoodsDao foodOrderGoodsDao;
    @Autowired
    private FoodGoodsCheckDao foodGoodsCheckDao;


    @Override
    @Transactional
    public String saveFoodOrderInfo(FoodOrderVO vo) {
        List<FoodOrderGoodsVO> goodsList =  vo.getGoodsList();
        if(goodsList==null || goodsList.size()==0){
            //商品不能为空
            return "-9";
        }
        Random random = new Random(100);
        String orderNo = DateUtil.format(new Date(),"yyyyMMddhhmmssSSS")+random.nextInt(10);
        for (FoodOrderGoodsVO goodsVO : goodsList){
            goodsVO.setOrderNo(orderNo);
            goodsVO.setCreatedTime(new Date());
            goodsVO.setCreateBy(vo.getCreateBy());
            foodOrderGoodsDao.insert(goodsVO);
        }
        vo.setOrderNo(orderNo);
        vo.setCreatedTime(new Date());
        int num = foodOrderDao.insert(vo);
        if(num>0){
            return orderNo;
        }
        return null;
    }

    @Override
    public List<FoodOrderVO> getOrderList(FoodOrderVO vo) {
        return foodOrderDao.getOrderList(vo);
    }

    @Override
    public List<FoodOrderGoodsVO> getOrderGoodsList(String orderNo) {
        List<FoodOrderGoodsVO> goodsList = foodOrderDao.getOrderGoodsList(orderNo);
        return goodsList;
    }

    @Override
    public List<FoodGoodsCheckVO> getGoodsCheckListByOrderNo(String orderNo) {
        List<FoodGoodsCheckVO> list =foodGoodsCheckDao.getGoodsCheckListByOrderNo(orderNo);
        return list;
    }

    @Override
    public int saveGoodsCheckInfo(FoodGoodsCheckVO vo) {
        vo.setCreatedTime(new Date());
        vo.setCreateBy(SysUserUtils.getSysUser().getUsername());
        return foodGoodsCheckDao.insert(vo);
    }

    @Override
    public List<FoodGoodsCheckVO> getOrderGoodsCheckList(String orderNo) {
        return foodGoodsCheckDao.getGoodsCheckListByOrderNo(orderNo);
    }

    @Override
    public int confirmOrderReceiv(FoodOrderVO vo) {
        int num=0;
        vo.setOrderReceivDate(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        if(!StringUtils.isEmpty(vo.getId())){
            vo.setModifyTime(new Date());
            num = foodOrderDao.updateById(vo);
        }
        return num;
    }

    @Override
    public CommonPage<FoodOrderVO> getPage(FoodOrderVO vo) {
        int pageSize = vo.getPageSize();
        int pageNum = vo.getPageNum();

        List<FoodOrderVO> list = this.foodOrderDao.getOrderPageList(vo, (pageNum - 1) * pageSize, pageSize);

        CommonPage commonPage = CommonPage.getCommonPage(pageNum, pageSize, this.foodOrderDao.getOrderPageListCount(vo), list);

        return commonPage;

    }

    @Override
    public CanteenDashBoradVO getOrderGoodsTop10() {
        List<FoodOrderGoodsVO> list = foodOrderDao.getOrderGoodsTop10();
        List<String> goodsList = new ArrayList<>();
        List<String> goodsNumberList = new ArrayList<>();
        CanteenDashBoradVO dashBoradVO = new CanteenDashBoradVO();
        for(FoodOrderGoodsVO vo: list){
            goodsList.add(vo.getGoodsName());
            goodsNumberList.add(vo.getGoodsNumber());
        }
        dashBoradVO.setGoodsList(goodsList);
        dashBoradVO.setGoodsNumberList(goodsNumberList);
        return dashBoradVO;
    }

    @Override
    public Map<String, Object> getWeekCheckPass() {
        Map<String,Object> resultMap = new HashMap<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dateList = new ArrayList<>();
        Date nowDate = new Date();
        for(int i=-7;i<0;i++){
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH,i);
            String date = simpleDateFormat.format(calendar.getTime());
            dateList.add(date);
        }
        List<Map<String,Object>> list = foodOrderDao.getWeekCheckPass(dateList);
        List<String> resutDateList = new ArrayList<>();
        List<String> passList = new ArrayList<>();
        List<String> noPassList = new ArrayList<>();
        for(Map<String,Object> map : list){
            resutDateList.add(String.valueOf(map.get("passDate")));
            passList.add(String.valueOf(map.get("pass")));
            noPassList.add(String.valueOf(map.get("noPass")));
        }
        resultMap.put("passDate",resutDateList);
        resultMap.put("passList",passList);
        resultMap.put("noPassList",noPassList);
        return resultMap;
    }

    @Override
    public Map<String, Object> getSpplierCheckPass(Integer limit) {
        List<Map<String,Object>> list = foodOrderDao.getSpplierCheckPass(limit);
        Map<String,Object> resultMap = new HashMap<>();
        List<String> spplierList = new ArrayList<>();
        List<String> passList = new ArrayList<>();
        List<String> noPassList = new ArrayList<>();
        for(Map<String,Object> map:list){
            spplierList.add(String.valueOf(map.get("supplier")));
            passList.add(String.valueOf(map.get("pass")));
            noPassList.add(String.valueOf(map.get("noPass")));
        }
        resultMap.put("spplierList",spplierList);
        resultMap.put("passList",passList);
        resultMap.put("noPassList",noPassList);
        return resultMap;
    }
}
